﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Api.Model;

namespace Api.Web.Tests
{
    [TestClass]
    public class Transaction
    {
        /// <summary>
        /// 實例化兩個DbContext，分別提交
        /// </summary>
        [TestMethod]
        public void Transaction1()
        {
            //這種情況下，是兩個不同的Db對象所以沒有事務的東西，沒有關聯
            Db db1 = new Db();
            Db db2 = new Db();
            db1.Projects.Add(new Project {Name = "test1234"});
            db2.Methods.Add(new Method());
            db1.SaveChanges();
            db2.SaveChanges();
        }

        [TestMethod]
        public void Transaction2()
        {
            //ok,回滾
            Db db1 = new Db();
            db1.Projects.Add(new Project { Name = "test1234" });
            db1.SaveChanges(); //每一次这个saveChange是一个transaction
            db1.Methods.Add(new Method() {CreateDate =null});//失敗
            db1.SaveChanges();
        }
    }
}
